package com.lebron.kotlin.ui.model.impl

import com.lebron.kotlin.bean.movie.TheaterBean
import com.lebron.kotlin.bean.movie.USTheaterBean
import com.lebron.kotlin.http.ApiCompleteListener
import com.lebron.kotlin.http.BaseObserver
import com.lebron.kotlin.http.DouBanApiManager
import com.lebron.kotlin.http.DouBanApiService
import com.lebron.kotlin.ui.model.MovieListModelI
import com.lebron.kotlin.ui.present.impl.MovieListPresentImpl
import com.lebron.kotlin.utils.Constants
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @Author lebron
 * @Date 18-7-26-下午4:02
 * 首页M层
 */
class MovieListModelImpl(val listener: ApiCompleteListener<TheaterBean,USTheaterBean>) : MovieListModelI{
    override fun getComingSoon(start: Int, count: Int) {
        DouBanApiManager.createService(Constants.DOUBAN_URL, DouBanApiService::class.java)
                ?.getComingSoon(start,count)
                ?.subscribeOn(Schedulers.io())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object: BaseObserver<TheaterBean>(){
                    override fun onNext(t: TheaterBean) {
                        listener.onComplete(t)
                    }
                    override fun onError(e: Throwable) {
                        listener.onFailed(e.message.toString())
                    }
                })
    }

    override fun getTop250(start: Int, count: Int) {
        DouBanApiManager.createService(Constants.DOUBAN_URL, DouBanApiService::class.java)
                ?.getTop250(start,count)
                ?.subscribeOn(Schedulers.io())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object: BaseObserver<TheaterBean>(){
                    override fun onNext(t: TheaterBean) {
                        listener.onComplete(t)
                    }
                    override fun onError(e: Throwable) {
                        listener.onFailed(e.message.toString())
                    }
                })
    }

    override fun getUSBox(start: Int, count: Int) {
        DouBanApiManager.createService(Constants.DOUBAN_URL, DouBanApiService::class.java)
                ?.getUSBox(start,count)
                ?.subscribeOn(Schedulers.io())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object: BaseObserver<USTheaterBean>(){
                    override fun onNext(t: USTheaterBean) {
                        listener.onSuccess(t)
                    }
                    override fun onError(e: Throwable) {
                        listener.onFailed(e.message.toString())
                    }
                })
    }

    override fun getTheater(start:Int,count:Int) {
        DouBanApiManager.createService(Constants.DOUBAN_URL, DouBanApiService::class.java)
                ?.getInTheater(start,count)
                ?.subscribeOn(Schedulers.io())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object: BaseObserver<TheaterBean>(){
                    override fun onNext(t: TheaterBean) {
                        listener.onComplete(t)
                    }
                    override fun onError(e: Throwable) {
                        listener.onFailed(e.message.toString())
                    }
                })
    }
}